package team.gs.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

import team.gs.Listener.sessionListener;
import team.gs.Messenger.Messenger;
import team.gs.model.Share;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import com.example.paralletkill.R;

public class Session extends Thread {

	public sessionListener sessionListener;
	public int userID;
	public Messenger itsMessenger;
	public Socket itsSocket;
	public static final int HEADER_LEN = 4;
	public static final int[] key = { 78, 103, 117, 121, 101, 110, 86, 97, 110,
			77, 105, 110, 104 };
	public byte providerId = (byte) 0;
	public boolean isAgent = false;
	public String capchaText = "";
	public boolean isRun = true;
	public short tableViewID = (short) -1;
	DataInputStream DIS = null;
	DataOutputStream DUS = null;
	public long lastTimeSend = -1L;
	public int version = 0;

	public Session(Socket socket1) {

		itsSocket = socket1;
		itsMessenger = new Messenger();
		try {
			DIS = new DataInputStream(socket1.getInputStream());
			DUS = new DataOutputStream(socket1.getOutputStream());
			start();
		} catch (Exception exception1) {
			sessionListener.exception(new Exception(
					"Không thể mở luồng kết nối"));
			System.out.println((new StringBuffer()).append("Lỗi: ").append(
					exception1));
		}

	}

	public void setSessionListener(sessionListener ses) {
		this.sessionListener = ses;
	}

	public void run() {
		while (isRun) {
			System.out.println("dang trog vong lap");
			Message message1 = null;
			try {
				message1 = getMessage();
				if (message1 != null) {
					lastTimeSend = System.currentTimeMillis();
					message1.ses = this;
					try {
//						System.out.println("seacccccccccccccccccccccccccccc = "
//								+ myIO.readByte(message1));
						itsMessenger.processMessage(message1, this);
						continue;
					} catch (Exception exception1) {
						// exception1.printStackTrace();
						sessionListener.exception(new Exception(
								"Lỗi khi gửi tin"));
						System.out.println((new StringBuffer()).append("Lỗi: ")
								.append(exception1));
						continue;
					}
				} else {
					isRun = false;
					break;
				}
			} catch (Exception exception2) {
				sessionListener.exception(new Exception("Lỗi khi gửi tin"));
				System.out.println((new StringBuffer()).append(":Lỗi: ")
						.append(exception2));
				isRun = false;
				continue;
			}
		}
		isRun = false;
		System.out.println("NGAT KET NOI");

	}

	private Message getMessage() throws Exception {
		int j = 0;
		byte[] ab = new byte[4];
		System.out.println("hien thi thong tin goi tin");
		while (j > -1 && j < 4) {
			int k = DIS.read(ab, j, 4 - j);

			j += k;
		}
		if (j == -1) {
			return null;
		} else {
			byte b1 = DIS.readByte();
			int i = 0;
			byte[] ab1 = null;

			j = 0;
			i = myIO.bytes2Int(ab);
			j = 0;
			ab1 = new byte[i];
			while (j > -1 && j < i) {
				int i1 = DIS.read(ab1, j, i - j);

				j += i1;
			}
			if (j == -1) {
				return null;
			} else {
				Message message1 = new Message(b1, ab1);
				return message1;
			}
		}
	}

	public synchronized void sendMessage(Message message1) {
		try {
			byte[] ab = message1.getBuffer().getData();
			byte b1 = message1.getCmd();

			if (ab.length > 0) {
				int i = ab.length;
				System.out.println("day thong tin len server");
				DUS.write(myIO.int2Bytes(i), 0, 4);
				DUS.writeByte(b1);
				DUS.write(ab, 0, i);
				DUS.flush();
			}
		} catch (Exception exception1) {
			exception1.printStackTrace();
		}
	}
}
